#include <iostream>
#include <string>
#include <cctype>

using namespace std;

int main()
{
    int ret = 0;
    string S;
    int n;
    cin >> n;
    while (n--)
    {
        cin >> S;
        double val = 0.0;
        double ret = 0;
        for (auto i : S)
        {
            if (i == 'C')
            {
                val = 12.01;
            }
            else if (i == 'H')
            {
                val = 1.008;
            }
            else if (i == 'O')
            {
                val = 16.00;
            }
            else if (i == 'N')
            {
                val = 14.01;
            }
            if (isdigit(i))
            {
                val = (i - '0' - 1) * val;
            }
            ret += val;
        }
        cout << ret << endl;
    }
}